﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarsModel;
using System.Data;
using System.Data.SqlClient;
using Lib.Data;
namespace CarsDal
{
    public class IndentsDal
    {
        public static string ConnectionString = "Data Source=47.93.36.227;Initial Catalog=SecondHandCars;User ID=Yang;Pwd=qaz@WSXedc";
        SqlConnection con = new SqlConnection(ConnectionString);
        /// <summary>
        /// 购物车显示方法
        /// </summary>
        /// <returns>list集合</returns>
        public List<Indents> IndentShow(int id)
        {
            string sql = "select * from Indents where UsersID=" + id;
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataAdapter ter = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            ter.Fill(dt);
            ter.Dispose();
            List<Indents> list = DataTableToList.DataTableToLista<Indents>(dt);
            return list;
        }
        /// <summary>
        /// 购物车分页显示方法
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="Pagesize">每页条数</param>
        /// <param name="PageCount">总页数</param>
        /// <returns></returns>
        public List<Indents> IndentFenYeShow(int PageIndex, int Pagesize, int UserID, ref int PageCount)
        {
            string sql = " exec IndentsFenYe " + PageIndex + "," + Pagesize + "," + UserID + "";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataAdapter ter = new SqlDataAdapter(com);
            DataSet set = new DataSet();
            ter.Fill(set);
            DataTable dtcount = set.Tables[0];
            PageCount = Convert.ToInt32(dtcount.Rows[0][0]);
            DataTable dt = set.Tables[1];
            ter.Dispose();
            List<Indents> list = DataTableToList.DataTableToLista<Indents>(dt);
            return list;
        }
        /// <summary>
        /// 购买方法
        /// </summary>
        /// <param name="s">实体</param>
        /// <returns>受影响行数</returns>
        public int IndentAdd(Indents s)
        {
            string sql = SqlADDString<Indents>.ADD(s);
            SqlCommand com = new SqlCommand(sql, con);
            int i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 购买后修改状态
        /// </summary>
        /// <param name="state">发货修改为1  收货修改为2</param>
        /// <param name="id">主键id</param>
        /// <returns>受影响行数</returns>
        public int IndentUpdate(int state, int id)
        {
            string sql = "update Indents set IndentState=" + state + " where IndentId=" + id;
            SqlCommand com = new SqlCommand(sql, con);
            int i = com.ExecuteNonQuery();
            return i;
        }
    }
}
